package com.itheima;

import com.itheima.manage_system.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class SutdentTool {

    //查询学生s_id索引,不存在返回-1,存在返回索引值
    public int getIndex(String in, ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            if (in.equals(list.get(i).getS_id())) {
                return i;
            }
        }
        return -1;
    }

    //添加学生
    public void add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student st = new Student();

//        String tempId =null;
        while (true) {
            System.out.println("请输入学生编号:");
            String s_id = sc.next();
            int index = getIndex(s_id, list);
            if (index != -1) {
                System.out.println("编号:" + s_id + " 已经存在,请重新输入!");
                continue;
            } else {
                System.out.println("请输入学生名字:");
                String name = sc.next();
                System.out.println("请输入学生年龄:");
                int age = sc.nextInt();
                System.out.println("请输入学生出生日期:");
                String birthday = sc.next();

                st.setS_id(s_id);
                st.setName(name);
                st.setAge(age);
                st.setBirthday(birthday);
                list.add(st);
                break;
            }
        }
    }

    //删除学生
    public void remove(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要删除的学生编号:");
            String s_id = sc.next();
            int index = getIndex(s_id, list);
            if (index == -1) {
                System.out.println("编号:" + s_id + " 不存在");
                return;
            } else {
                list.remove(index);
                System.out.println("删除成功");
                return;
            }
        }

    }

    //修改学生
    public void update(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要修改的学生编号:");
            String s_id = sc.next();
            int index = getIndex(s_id, list);
            if (index == -1) {
                System.out.println("编号:" + s_id + " 不存在");
                return;
            } else {
                Student st = list.get(index);
                System.out.println("请输入学生名字:");
                st.setName(sc.next());
                System.out.println("请输入学生年龄:");
                st.setAge(sc.nextInt());
                System.out.println("请输入学生出生日期:");
                st.setBirthday(sc.next());
                System.out.println("修改成功");
                return;
            }
        }
    }

    //查询学生
    public void queryAll(ArrayList<Student> list) {
        if (list.isEmpty()||list.size() == 0) {
            System.out.println("暂无学生,请添加后再试!");
        } else {
            System.out.println("编号\t\t\t姓名\t\t\t年龄\t\t\t出生日期");
            for (int i = 0; i < list.size(); i++) {
                Student st = list.get(i);
                System.out.println(st.getS_id() + "\t\t\t" + st.getName()
                        + "\t\t\t" + st.getAge() + "\t\t\t" + st.getBirthday());
            }

        }
    }
}
